# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libxml2

pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.9.1
pkgrel=3
arch=('any')
groups=("${MINGW_PACKAGE_PREFIX}")
pkgdesc="XML parsing library, version 2 (mingw-w64)"
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs" "${MINGW_PACKAGE_PREFIX}-gettext" "${MINGW_PACKAGE_PREFIX}-zlib" "${MINGW_PACKAGE_PREFIX}-xz")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config" "${MINGW_PACKAGE_PREFIX}-python2")
options=('strip' 'staticlibs')
license=('LGPL')
url="http://www.xmlsoft.org/"
install=${_realname}-${CARCH}.install
source=("http://xmlsoft.org/sources/libxml2-${pkgver}.tar.gz"
        "mingw32-libxml2-static-build-compile-fix.patch"
    "libxml2-2.8.0_rc1-winnt.patch"
    "libxml2-2.9.0-largefile.patch"
    "libxml2-2.9.0-man_fixes.patch"
    "libxml2-2.9.0-open.gz.patch"
    "libxml2-2.9.1-disable_static_modules.patch"
    0004-detect-ws2tcpip.mingw.patch
    0005-fix-char-cast-warning.mingw.patch
    0006-fix-unused-var-warning.mingw.patch
    0007-fix-stat-redefinition.mingw.patch
    0008-include-winsock2.h-before-windows.h.mingw.patch
    0009-use-wstat-appropriate-for-mingw-w64.mingw.patch
    0010-mingw-w64-defines-lots-of-errnos.mingw.patch
    0011-more-winsock-inclusion-order-fixes.mingw.patch
    0012-socklen-is-signed-on-mingw-w64.mingw.patch
    0013-fix-field-type-signedness.all.patch
    0014-fix-prototype-warning.all.patch
    0015-fix-unused-parameters-warning.all.patch
    0016-WARNING-to-be-fixed.all.patch
    0017-fix-const-warning.all.patch
    0018-function-declaration-isnt-prototype.all.patch
    0019-unused-flags.all.patch
    0020-fix-size_t-format-specifier.all.patch
    0023-fix-sitedir-detection.mingw.patch
    0024-shrext-pyd.mingw.patch
    0025-mingw-python-dont-remove-dot.patch)
md5sums=('9c0cfef285d5c4a5c80d00904ddab380'
         '0df377025082cd93cccbca547f048011'
         '2a38e25bead58e4b4dc4550317369c90'
         '945af4cc9a596630ac391ce90871e521'
         'eb14f4ce77ac4c55d0262b6be3011913'
         '9fd252fcb751fdf72688fe0cddeb373e'
         '98ba4981c95bb51a854232dd098a99e8'
         '102523d60cb896a3fb558b3f999f6edd'
         'f09fc3dc4a01d81230c37cd49d1d5bcb'
         'e526d4e285579c5c8b9c61e889338454'
         '5588d05865d113a81b0f2c514a7f0d0c'
         '9d2e2e1dfb462ea9ef8216f94bd40cb7'
         '1840d1087f7c3b21219b9d196c5a5930'
         '2784f4e947952ffc3475c111acb72d01'
         'ad9137ca850b62ada756a1706bcc65e1'
         '6b65d303079b21e1c93ec169d5f3f4d0'
         '543b01b756fa64fafb20819d20c984e6'
         '9a56b69cc5b1a7fc5d4eb0bab17ee4df'
         '6adb6886030e33bd40c29724bece6ee8'
         'd9e7c8cbc18c76575fd870ca2e39b66a'
         'de11e27ada1d7408aae9b54ed77aafe6'
         'af8b3dbb628682b1b14272fa314b8cd6'
         '30610488e5c8e98f22e226541c95eca9'
         'cc26dda9ff1ddebfc5601b476244fdf6'
         'c721db278ecc09154e233b329a8e4cd3'
         '0b3858d4f608711d1051c050d4452b1b'
         'ec3aa7c1a6bcdd360cde59305c121dd4')

prepare() {
  cd "${srcdir}/libxml2-${pkgver}"

  #patch -Np0 -i "${srcdir}"/mingw32-libxml2-static-build-compile-fix.patch
  #patch -p1 -i ${srcdir}/libxml2-2.9.1-disable_static_modules.patch
  #patch -p1 -i ${srcdir}/libxml2-2.8.0_rc1-winnt.patch
  patch -p1 -i ${srcdir}/libxml2-2.9.0-largefile.patch
  patch -p1 -i ${srcdir}/libxml2-2.9.0-man_fixes.patch
  patch -p1 -i ${srcdir}/libxml2-2.9.0-open.gz.patch
  
  patch -p1 -i ${srcdir}/0004-detect-ws2tcpip.mingw.patch
  patch -p1 -i ${srcdir}/0005-fix-char-cast-warning.mingw.patch
  patch -p1 -i ${srcdir}/0006-fix-unused-var-warning.mingw.patch
  patch -p1 -i ${srcdir}/0007-fix-stat-redefinition.mingw.patch
  patch -p1 -i ${srcdir}/0008-include-winsock2.h-before-windows.h.mingw.patch
  patch -p1 -i ${srcdir}/0009-use-wstat-appropriate-for-mingw-w64.mingw.patch
  patch -p1 -i ${srcdir}/0010-mingw-w64-defines-lots-of-errnos.mingw.patch
  patch -p1 -i ${srcdir}/0011-more-winsock-inclusion-order-fixes.mingw.patch
  patch -p1 -i ${srcdir}/0012-socklen-is-signed-on-mingw-w64.mingw.patch
  patch -p1 -i ${srcdir}/0013-fix-field-type-signedness.all.patch
  patch -p1 -i ${srcdir}/0014-fix-prototype-warning.all.patch
  patch -p1 -i ${srcdir}/0015-fix-unused-parameters-warning.all.patch
  patch -p1 -i ${srcdir}/0016-WARNING-to-be-fixed.all.patch
  patch -p1 -i ${srcdir}/0017-fix-const-warning.all.patch
  patch -p1 -i ${srcdir}/0018-function-declaration-isnt-prototype.all.patch
  patch -p1 -i ${srcdir}/0019-unused-flags.all.patch
  patch -p1 -i ${srcdir}/0020-fix-size_t-format-specifier.all.patch
  patch -p1 -i ${srcdir}/0023-fix-sitedir-detection.mingw.patch
  patch -p1 -i ${srcdir}/0024-shrext-pyd.mingw.patch
  patch -p1 -i ${srcdir}/0025-mingw-python-dont-remove-dot.patch
  
  libtoolize --copy --force
  aclocal
  automake --add-missing
  autoconf
}

build()
{
  export LDFLAGS+=" -Wl,-no-undefined"
  export CFLAGS+=" -fexceptions --param=ssp-buffer-size=4"

  # Static build
    mkdir -p ${srcdir}/build-static-${CARCH} && cd ${srcdir}/build-static-${CARCH}
    ../libxml2-${pkgver}/configure \
      --prefix=${MINGW_PREFIX} \
      --host=${MINGW_CHOST} \
      --target=${MINGW_CHOST} \
      --build="${MINGW_CHOST}" \
      --without-python \
      --with-modules \
      --enable-static \
      --disable-shared \
      --with-threads=win32 \
    CFLAGS="${CFLAGS} -DLIBXML_STATIC_FOR_DLL"
    make

  # Shared build
    mkdir -p ${srcdir}/build-shared-${CARCH} && cd ${srcdir}/build-shared-${CARCH}
    ${srcdir}/libxml2-${pkgver}/configure \
      --prefix=${MINGW_PREFIX} \
      --host=${MINGW_CHOST} \
      --target=${MINGW_CHOST} \
      --build="${MINGW_CHOST}" \
      --with-python=${MINGW_PREFIX}/bin/python2 \
      --with-modules \
      --disable-static \
      --enable-shared \
      --with-threads=win32 \
    CFLAGS="${CFLAGS} -D__USE_MINGW_ANSI_STDIO=1" \
    CXXFLAGS="${CXXFLAGS} -D__USE_MINGW_ANSI_STDIO=1"
    
  # Hack for mingw python
  pushd ${MINGW_PREFIX} > /dev/null
  export PREFIX_DEPS=`pwd -W`
  popd > /dev/null
  find . -type f -name "Makefile" -exec sed -i "s|${PREFIX_DEPS}|${MINGW_PREFIX}|g" {} \;
    make
}

package()
{
  cd "${srcdir}/libxml2-${pkgver}"

  # First install shared
  cd ${srcdir}/build-shared-${CARCH}

  make install DESTDIR="${pkgdir}"

  # Remove documentation
  #rm -rf "${pkgdir}${MINGW_PREFIX}"/share/man
  #rm -rf "${pkgdir}${MINGW_PREFIX}"/share/doc
  #rm -rf "${pkgdir}${MINGW_PREFIX}"/share/gtk-doc

  # then manually install static
  cd ${srcdir}/build-static-${CARCH}

  install -m 0644 .libs/libxml2.a "${pkgdir}${MINGW_PREFIX}"/lib/
}
